#pragma once
#include <string>
#include <functional>
#include "InetAddr.hpp"

class Connection;
class Reactor;
using func_t = std::function<void (Connection*)>;

//将文件描述符当作是Connection对象来看
class Connection
{
public:
    Connection(int sockfd)
        :_sockfd(sockfd)
    {}
    
    void SetEvents(uint32_t events)
    {
        _events=events;
    }

    uint32_t Events()
    {
        return _events;
    }

    void Register(func_t recver,func_t sender,func_t excepeter)
    {
        _recver=recver;
        _sender=sender;
        _excepeter=excepeter;
    }

    void SetSelf(Reactor* self)
    {
        _self=self;
    }

    int SockFd()
    {
        return _sockfd;
    }

    void AppendInbuffer(const std::string& buff)
    {
        _inbuffer+=buff;
    }

    std::string& InBuffer()
    {
        return _inbuffer;    
    }

    func_t _recver;
    func_t _sender;
    func_t _excepeter;
    Reactor* _self;//这样可以通过Connection(对文件描述符的封装)找到reactor

private:
    int _sockfd;
    std::string _inbuffer;
    std::string _outbuffer;

    uint32_t _events;//Connection对象中,sockfd关心的事件集合

    InetAddr _addr;

};